package custom.alfresco.script.node.extension;

import org.alfresco.repo.jscript.ScriptNode;
import org.alfresco.repo.processor.BaseProcessorExtension;
import org.alfresco.service.cmr.lock.LockService;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;

/**
 * 
 * @author Rui Fernandes
 *
 */
public class UnlockDelete extends BaseProcessorExtension {
	
	
	private NodeService nodeService;
	private LockService lockService;

	public void setLockService(LockService lockService) {
		this.lockService = lockService;
	}

	public void setNodeService(NodeService nodeService) {
		this.nodeService = nodeService;
	}
	
	
	public void unlockDeleteContents(ScriptNode node){
		for(ChildAssociationRef child:nodeService.getChildAssocs(node.getNodeRef())){
			iterativeUnlockDelete(child.getChildRef());
		}
	}

	public void unlockDelete(ScriptNode node) {
		iterativeUnlockDelete(node.getNodeRef());
		
	}
	
	
	private void iterativeUnlockDelete(NodeRef node) {
		for(ChildAssociationRef child:nodeService.getChildAssocs(node)){
			iterativeUnlockDelete(child.getChildRef());
		}
		lockService.unlock(node);
		nodeService.deleteNode(node);
	}

}
